package com.cloudspace.jindun.activity.Woyao;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.cloudspace.jindun.R;
import com.cloudspace.jindun.activity.CustomTitleBaseActivity;
import com.cloudspace.jindun.adapter.ReportListAdapter;
import com.cloudspace.jindun.model.RepData;
import com.cloudspace.jindun.network.url.YXHttpUrl;
import com.cloudspace.jindun.user.UserSessionManager;
import com.google.gson.Gson;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import butterknife.OnClick;
import okhttp3.Call;

//汇报记录
public class ReportActivity extends CustomTitleBaseActivity {

    ListView mReportListViewData;
    private ReportListAdapter mReportListAdapter;
    private RepData mRepData;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_report, true);
        getTitleBar().setSimpleMode(getResources().getString(R.string.wy_sbjl));

        mReportListViewData = (ListView) findViewById(R.id.report_listView_data);

        initData();
        setData();
        setListener();
    }

    private void setData() {
        mReportListViewData.setAdapter(mReportListAdapter);

    }

    private void initData() {
        mReportListAdapter = new ReportListAdapter(this);
    }

    @Override
    protected void onStart() {
        super.onStart();
        if (mRepData != null) {
            mReportListAdapter.clear();
        }

        requestData(YXHttpUrl.WY_INTELLIGENCE_IN, UserSessionManager.getInstance().getUserId());
    }


    private void setListener() {
        mReportListViewData.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (mRepData != null) {
                    Intent intent = new Intent();
                    intent.setClass(ReportActivity.this, ReportdetailsActivity.class);
                    Bundle bundle = new Bundle();
                    bundle.putSerializable("data",  mRepData.getData().get(position));
                    intent.putExtras(bundle);
                    startActivity(intent);
                }
            }
        });

    }

    private void requestData(String url, final String reportId) {
        OkHttpUtils.get()
                .url(url + reportId)
                .build()
                .execute(new StringCallback() {

                    @Override
                    public void onError(Call call, Exception e, int id) {

                    }

                    @Override
                    public void onResponse(String response, int id) {
                        if (response != null) {
                            Gson gson = new Gson();
                            mRepData = gson.fromJson(response, RepData.class);
                            mReportListAdapter.addAll(mRepData.getData());
                        }

                    }
                });
    }

    @OnClick(R.id.back)
    public void onClick() {
    }
}
